package com.nyist.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.nyist.service.OrderHystrixUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;


@RestController
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Resource
    private OrderHystrixUserService service;

    @GetMapping("/consumer/hystrix/ok/{id}")
    public String printInfo_OK(@PathVariable("id") Integer id){
        String infoOk = service.printInfo_OK(id);
        return infoOk;
    }

    @GetMapping("/consumer/hystrix/globalfallback/{id}")
    @HystrixCommand
    public String printInfo_GlobalFallBack(@PathVariable("id") Integer id){
        String infoOk = service.printInfo_OK(id);
        return infoOk;
    }

    @GetMapping("/consumer/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "printInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
    })
    public String printInfo_TimeOut(@PathVariable("id") Integer id){
        String infoTimeOut = service.printInfo_TimeOut(id);
        return infoTimeOut;
    }

    /**
     *  服务降级处理
     * @param id
     * @return
     */
    public String printInfo_TimeOutHandler(@PathVariable("id") Integer id){
        return "订单模块80：系统繁忙请稍后再试或运行报错    printInfo_TimeOutHandler,id"+id+"\t"+"呜呜呜";
    }

    /**
     *  全局服务降级处理  全局的没有参数 ，因为要处理所有方法
     *
     * @return
     */
    public String payment_Global_FallbackMethod(){
        return "订单模块80：全局处理------系统繁忙请稍后再试或运行报错   呜呜呜";
    }

}
